/**
 * All rights Reserved, Designed By Android_Robot
 *
 * @Title: EquipDetailActivity.java
 * @Package com.jiyi.activity.owner
 * @Description: TODO(用一句话描述该文件做什么)
 * @author: HYM
 * @date: 2017-9-13 上午9:30:14
 * @version V1.0
 */
package com.jiyi.activity.new_maintenance;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.android.volley.VolleyError;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jiyi.adapter.CommonAdapter;
import com.jiyi.adapter.ViewHolder;
import com.jiyi.bean.maintenance.EquipSystemBean;
import com.jiyi.bean.maintenance.SystemEquipItemBean;
import com.jiyi.bean.owner.PlanBean;
import com.jiyi.bean.owner.PlanBean.PlanEquip;
import com.jiyi.bean.owner.PositionPoint;
import com.jiyi.bean.weibao.CsszLcInfoBean.EquipType;
import com.jiyi.common.StringInterface;
import com.jiyi.common.VolleyRequest;
import com.jiyi.easemob.util.UserHelp;
import com.jiyi.inteface.PhotoClickInterface;
import com.jiyi.inteface.WebApi;
import com.jiyi.widget.CanTouchImageView;
import com.jiyi.widget.ClearEditText;
import com.jiyi.widget.OverSurfaceView;
import com.orhanobut.logger.Logger;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppBase;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.CheckUtil;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;
import com.swift.utils.DeviceUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: EquipDetailActivity
 * @Description:TODO(设备详情)
 * @author: HYM
 * @date: 2017-9-13 上午9:30:14
 *
 */
public class EquipDetailActivity extends ActivityBase {

    /* 头部 */
    @BindView(R.id.tvBack)
    TextView tvBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvSearch)
    TextView tvSearch;

    @BindView(R.id.parentView)
    ScrollView parentView;
    @BindView(R.id.llMap)
    LinearLayout llMap;
    @BindView(R.id.rlPoint)
    RelativeLayout rlPoint;
    @BindView(R.id.tvEditEquip)
    TextView tvEditEquip;
    @BindView(R.id.tvDelEquip)
    TextView tvDelEquip;
    @BindView(R.id.llDisplay)
    LinearLayout llDisplay;
    @BindView(R.id.llEdit)
    LinearLayout llEdit;
    @BindView(R.id.tvPost)
    TextView tvPost;
    @BindView(R.id.tvPoint)
    TextView tvPoint;
    @BindView(R.id.llCanclePoint)
    LinearLayout llCanclePoint;
    @BindView(R.id.ivPic)
    CanTouchImageView ivPic;

    @BindView(R.id.tvGroup)
    TextView tvGroup;
    @BindView(R.id.tvSystem)
    TextView tvSystem;
    @BindView(R.id.tvEquip)
    TextView tvEquip;
    @BindView(R.id.llEquipInfo)
    LinearLayout llEquipInfo;
    @BindView(R.id.tvEquipNo)
    TextView tvEquipNo;
    @BindView(R.id.tvEquipName)
    TextView tvEquipName;
    @BindView(R.id.tvEquipPosition)
    TextView tvEquipPosition;
    @BindView(R.id.tvEquipBrand)
    TextView tvEquipBrand;
    @BindView(R.id.tvEquipModel)
    TextView tvEquipModel;
    @BindView(R.id.state_equip_type_add)
    Spinner state_equip_type_add;
    @BindView(R.id.state_system)
    Spinner state_system;
    @BindView(R.id.state_equip)
    Spinner state_equip;
    @BindView(R.id.tvEquipPrice)
    TextView tvEquipPrice;
    @BindView(R.id.tvX)
    TextView tvX;
    @BindView(R.id.tvY)
    TextView tvY;

    @BindView(R.id.etEquipNo)
    EditText etEquipNo;
    @BindView(R.id.etEquipName)
    EditText etEquipName;
    @BindView(R.id.etEquipPosition)
    EditText etEquipPosition;
    @BindView(R.id.tvEquipBrandEdit)
    TextView tvEquipBrandEdit;
    @BindView(R.id.tvEquipModelEdit)
    TextView tvEquipModelEdit;
    @BindView(R.id.tvEquipType)
    TextView tvEquipType;
    @BindView(R.id.etGroup)
    EditText etGroup;
    @BindView(R.id.etEquipPrice)
    EditText etEquipPrice;
    @BindView(R.id.textview_status)
    TextView tvStatus;
    @BindView(R.id.etSearch)
    ClearEditText etSearch;
    @BindView(R.id.tvEditX)
    TextView tvEditX;
    @BindView(R.id.tvEditY)
    TextView tvEditY;

    @BindView(R.id.tvBuilding)
    TextView tvBuilding;
    @BindView(R.id.tvLayout)
    TextView tvLayout;

    String title, rightButtonText, buildId, floorId;
    // 该页面有多个功能 0 显示 1 新增 2 编辑
    int func = 0;
    // 平面图
    OverSurfaceView osvPlan;

    final int ADD = 1;

    // 平面图对象
    PlanBean planBean;
    // 当前设备对象
    PlanEquip planEquip;
    // 修改前的设备临时对象
    PlanEquip tempUpdatePlanEquip;

    // 进度条
    Dialog dialog;

    // 系统列表适配器
    CommonAdapter<EquipSystemBean> stateSystemAdapter;
    // 系统列表数据源
    List<EquipSystemBean> systemDataList = new ArrayList<EquipSystemBean>();
    // 系统-设备列表适配器
    CommonAdapter<SystemEquipItemBean> stateEquipItemAdapter;
    // 系统-设备列表数据源
    List<SystemEquipItemBean> systemEquipItemDataList = new ArrayList<SystemEquipItemBean>();
    // 设备类型列表适配器
    CommonAdapter<EquipType> stateEquipTypeAdapter;
    // 设备类型列表数据源
    List<EquipType> equipTypeDataList = new ArrayList<EquipType>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_equip_detail);
        ButterKnife.bind(this);
        init();
    }

    @Override
    protected void onDestroy() {
        hideDialog(dialog);
        ivPic.stop();


        super.onDestroy();
    }

    private void init() {
        getIntentData();
        initViewState();
        setTop();
        setAdapter();
        getData();
        // initLayoutByFunc(func);
    }

    private void initViewState() {
        // 设置输入法的搜索事件
        etSearch.setOnEditorActionListener(new OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH
                        || actionId == EditorInfo.IME_ACTION_DONE) {
                    InputMethodManager imm = (InputMethodManager) v
                            .getContext().getSystemService(
                                    Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

                    Log.e("onEditorAction  ", "   done ");
                    searchCode();
                    return true;
                }
                return false;
            }
        });

    }

    /**
     * 平面图上 点 的点击回调
     */
    private void setChooseEquipViewStatus() {
        ivPic.setmPhotoClickInterface(new PhotoClickInterface() {

            @Override
            public void onPhotoClcik(float x, float y, float xRatio,
                                     float yRatio) {
                tvEditX.setText(xRatio + "");
                tvEditX.setText(yRatio + "");
            }

            @Override
            public void onPointClick(String equipment_id, float x, float y,
                                     float xRatio, float yRatio) {
                ivPic.reset();
                setChooseMaps(planBean.getEquipList(), equipment_id);
                getEquipInfo(equipment_id);
            }
        });

    }

    private void setAddEquipViewStatus() {
        ivPic.setmPhotoClickInterface(new PhotoClickInterface() {

            @Override
            public void onPhotoClcik(float x, float y, float xRatio,
                                     float yRatio) {
                tvEditX.setText(xRatio + "");
                tvEditY.setText(yRatio + "");
            }

            @Override
            public void onPointClick(String equipment_id, float x, float y,
                                     float xRatio, float yRatio) {
                // getEquipInfo(equipment_id);
            }
        });

        // 设置必填项目验证
        etEquipName.setTag(R.id.tag_view_empty, "名称不能为空");
        etEquipNo.setTag(R.id.tag_view_empty, "编码不能为空");
        etEquipPrice.setTag(R.id.tag_view_empty, "单价不能为空");
        etEquipPosition.setTag(R.id.tag_view_empty, "位置不能为空");
        state_equip_type_add.setTag(R.id.tag_view_empty, "设备类型不能为空");
        etGroup.setTag(R.id.tag_view_empty, "分组不能为空");
        state_system.setTag(R.id.tag_view_empty, "系统类型不能为空");
        state_equip.setTag(R.id.tag_view_empty, "系统设备类型不能为空");
        tvEditX.setTag(R.id.tag_view_empty, "请放置设备类型");
        tvEditX.setTag(R.id.tag_view_empty, "请放置设备类型");
    }

    private void setUpdateEquipViewStatus() {
        ivPic.setmPhotoClickInterface(new PhotoClickInterface() {

            @Override
            public void onPhotoClcik(float x, float y, float xRatio,
                                     float yRatio) {
                tvEditX.setText(xRatio + "");
                tvEditY.setText(yRatio + "");

                for (int i = 0; i < planBean.getEquipList().size(); i++) {
                    PlanEquip temp = planBean.getEquipList().get(i);
                    if (temp.getId().equals(planEquip.getId())) {
                        // 保存一份到临时的对象，如果取消编辑时还原
                        if (tempUpdatePlanEquip == null) {
                            tempUpdatePlanEquip = new PlanEquip();
                            tempUpdatePlanEquip.setId(temp.getId());
                            tempUpdatePlanEquip.setX_axis(temp.getX_axis());
                            tempUpdatePlanEquip.setY_axis(temp.getY_axis());
                            tempUpdatePlanEquip.setEquiptype(temp
                                    .getEquiptype());
                            tempUpdatePlanEquip.setIcon_name(temp
                                    .getIcon_name());
                            tempUpdatePlanEquip.setIcon(temp.getIcon());
                            tempUpdatePlanEquip.setGroupno(temp.getGroupno());
                            tempUpdatePlanEquip.setEquipment_id(temp
                                    .getEquipment_id());
                            tempUpdatePlanEquip.setEquipment_name(temp
                                    .getEquipment_name());
                            tempUpdatePlanEquip.setInspection_id(temp
                                    .getInspection_id());
                            tempUpdatePlanEquip.setInspection_name(temp
                                    .getInspection_name());

                        }

                        temp.setX_axis(xRatio);
                        temp.setY_axis(yRatio);
                        EquipType equipType = new Gson().fromJson(
                                state_equip_type_add.getSelectedItem()
                                        .toString(), EquipType.class);
                        temp.setEquiptype(equipType.getId());
                        temp.setIcon_name(equipType.getIcon_name());
                        temp.setIcon(equipType.getIcon_url());
                        break;
                    }
                }

                ivPic.reset();
                setChooseMaps(planBean.getEquipList(), planEquip.getId());
            }

            @Override
            public void onPointClick(String equipment_id, float x, float y,
                                     float xRatio, float yRatio) {
            }
        });

        // 设置必填项目验证
        etEquipName.setTag(R.id.tag_view_empty, "名称不能为空");
        etEquipNo.setTag(R.id.tag_view_empty, "编码不能为空");
        etEquipPrice.setTag(R.id.tag_view_empty, "单价不能为空");
        etEquipPosition.setTag(R.id.tag_view_empty, "位置不能为空");
        state_equip_type_add.setTag(R.id.tag_view_empty, "设备类型不能为空");
        etGroup.setTag(R.id.tag_view_empty, "分组不能为空");
        state_system.setTag(R.id.tag_view_empty, "系统类型不能为空");
        state_equip.setTag(R.id.tag_view_empty, "系统设备类型不能为空");
        tvEditX.setTag(R.id.tag_view_empty, "请放置设备类型");
        tvEditX.setTag(R.id.tag_view_empty, "请放置设备类型");
    }

    private void getData() {

        Log.d("WZS",floorId);
        Log.d("token", UserInfoUtil.getCurrentUserAccessToken());

        Map<String, String> params = new HashMap<String, String>();
        params.put(AppConfig.ACCESS_TOKEN,
                UserInfoUtil.getCurrentUserAccessToken());
        params.put("floor_id", floorId);

        dialog = showDialog(dialog);
        VolleyRequest.StringRequestPost(getContext(),
                WebApi.MAINTENANCE_OWNER_FLOOR_INFO, "", params,
                new StringInterface(getContext()) {

                    @Override
                    public void stringSuccess(String result) {
                        try {
                            JSONObject obj = new JSONObject(result);
                            String status = obj.getString("status");
                            String info = obj.getString("info");
                            if (status.equals("200")) {
                                String data = obj.getString("data");
                                planBean = new Gson().fromJson(data,
                                        PlanBean.class);


                                Logger.json(result);

                                for (int i = 0; i < planBean.getEquipList().size(); i++) {

//                                    if (i == 0 || i == 1) {
//                                        planBean.getEquipList().get(i).setAlarm_status(1);
//                                    } else if (i == planBean.getEquipList().size() - 2 || i == planBean.getEquipList().size() - 3) {
//                                        planBean.getEquipList().get(i).setAlarm_status(2);
//                                    } else {
//                                        planBean.getEquipList().get(i).setAlarm_status(0);
//                                    }
                                }

                                tvTitle.setText(planBean.getBuilding()
                                        + planBean.getFloor_no());
                                initLayoutByFunc(func);
                                Glide.with(getContext())
                                        .load(planBean.getMap())
                                        .asBitmap()
                                        // 加入了计算高度的方法
                                        .listener(
                                                new RequestListener<String, Bitmap>() {
                                                    @Override
                                                    public boolean onException(
                                                            Exception e,
                                                            String model,
                                                            Target<Bitmap> target,
                                                            boolean isFirstResource) {
                                                        return false;
                                                    }

                                                    @Override
                                                    public boolean onResourceReady(
                                                            Bitmap resource,
                                                            String model,
                                                            Target<Bitmap> target,
                                                            boolean isFromMemoryCache,
                                                            boolean isFirstResource) {
                                                        if (ivPic == null) {
                                                            return false;
                                                        }
                                                        LayoutParams params = ivPic
                                                                .getLayoutParams();
                                                        int vw = ivPic
                                                                .getWidth()
                                                                - ivPic.getPaddingLeft()
                                                                - ivPic.getPaddingRight();
                                                        Drawable drawable = new BitmapDrawable(
                                                                resource);
                                                        float scale = (float) vw
                                                                / (float) drawable
                                                                .getIntrinsicWidth();
                                                        int vh = Math.round(drawable
                                                                .getIntrinsicHeight()
                                                                * scale);
                                                        params.height = vh
                                                                + ivPic.getPaddingTop()
                                                                + ivPic.getPaddingBottom();
                                                        ivPic.setLayoutParams(params);
                                                        return false;
                                                    }
                                                })
                                        .into(new SimpleTarget<Bitmap>(
                                                DeviceUtil.getInstance()
                                                        .getScreenWidth() / 2,
                                                DeviceUtil.getInstance()
                                                        .getScreenHeight() / 2) {

                                            @Override
                                            public void onResourceReady(
                                                    Bitmap bitmap,
                                                    GlideAnimation glideAnimation) {
                                                ivPic.setImageBitmap(bitmap);
                                                ivPic.setBmp(bitmap);
                                                setMaps(planBean.getEquipList());
                                            }

                                            public void onLoadFailed(
                                                    Exception e,
                                                    Drawable errorDrawable) {
                                                ivPic.setImageBitmap(null);
                                                ivPic.setBmp(null);
                                            }

                                            ;
                                        });

                                // 设置品牌、规格
                                tvEquipBrandEdit.setText(planBean.getEquipment_brand());
                                tvEquipModelEdit.setText(planBean.getEquipment_model());
                                tvEquipBrand.setText(planBean.getEquipment_brand());
                                tvEquipModel.setText(planBean.getEquipment_model());
                            } else if (status.equals("404")
                                    && info.contains("尚未登录")) {
                                ToastUtil.getInstances().showMsg(
                                        "尚未登录或该用户在其他地方登录，请重新登录！");
                                // 清除环信登录
                                UserHelp.getInstance().clearUser(
                                        AppBase.Context, true);
                                return;
                            } else {
                                ToastUtil.getInstances().showMsg(info);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        } finally {
                            hideDialog(dialog);
                        }
                    }

                    @Override
                    public void stringError(VolleyError error) {
                        hideDialog(dialog);
                        ToastUtil.getInstances().showMsg(
                                R.string.err_http_response);
                    }
                });
    }

    private void setAdapter() {
        // switch (func) {
        // case 1:

        resetSystem();
        stateSystemAdapter = new CommonAdapter<EquipSystemBean>(this,
                systemDataList, R.layout.item_state_small) {

            @Override
            public void convert(ViewHolder holder, EquipSystemBean t) {
                holder.setText(R.id.state_name, t.getInspection_name());
            }
        };
        state_system.setAdapter(stateSystemAdapter);
        state_system.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                EquipSystemBean build = (EquipSystemBean) parent.getAdapter()
                        .getItem(position);
                state_system.setTag(R.id.tag_view_select_value,
                        build.getInspection_id());
                resetSystemEquipItem();
                // 做个判断，如果上级菜单传给我们的是空id，即上级未选择选项，则我们不去获取下级的菜单
                if (!TextUtils.isEmpty(systemDataList.get(position)
                        .getInspection_id())) {
                    getSystemEquipItemData(systemDataList.get(position)
                            .getInspection_id());
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        resetSystemEquipItem();
        stateEquipItemAdapter = new CommonAdapter<SystemEquipItemBean>(this,
                systemEquipItemDataList, R.layout.item_state_small) {

            @Override
            public void convert(ViewHolder holder, SystemEquipItemBean t) {
                holder.setText(R.id.state_name, t.getEquipment_name());
            }
        };
        state_equip.setAdapter(stateEquipItemAdapter);
        state_equip.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                SystemEquipItemBean systemEquipItemBean = (SystemEquipItemBean) parent
                        .getAdapter().getItem(position);
                state_equip.setTag(R.id.tag_view_select_value,
                        systemEquipItemBean.getEquipment_id());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        resetEquipType();
        stateEquipTypeAdapter = new CommonAdapter<EquipType>(this,
                equipTypeDataList, R.layout.item_state_small) {

            @Override
            public void convert(ViewHolder holder, EquipType t) {
                holder.setText(R.id.state_name, t.getIcon_name());
            }
        };
        state_equip_type_add.setAdapter(stateEquipTypeAdapter);
        state_equip_type_add
                .setOnItemSelectedListener(new OnItemSelectedListener() {

                    @Override
                    public void onItemSelected(AdapterView<?> parent,
                                               View view, int position, long id) {
                        EquipType et = (EquipType) parent.getAdapter().getItem(
                                position);
                        state_equip_type_add.setTag(R.id.tag_view_select_value,
                                et.getId());
                        Glide.with(getContext()).load(et.getIcon_url())
                                .asBitmap().into(new SimpleTarget<Bitmap>() {

                            @Override
                            public void onResourceReady(Bitmap bitmap,
                                                        GlideAnimation glideAnimation) {
                                ivPic.setWaterBitmap(bitmap);
                            }
                        });
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });

        // break;
        // case 2:
        //
        // break;
        // default:
        // break;
        // }

    }

    private void getIntentData() {
        floorId = getIntent().getStringExtra("floorId");
        func = getIntent().getIntExtra("func", 0);
        title = getIntent().getStringExtra("title");
        if (TextUtils.isEmpty(title)) {
            title = "新增设备";
            rightButtonText = "取消";
        } else {
            rightButtonText = "新增设备";
        }
    }

    private void setTop() {
        tvTitle.setText(title);
        tvSearch.setCompoundDrawables(null, null, null, null);
        tvSearch.setText(rightButtonText);
    }

    /**
     * @author: HYM
     * @Title: initLayoutByFunc
     * @Description: TODO(根据功能初始化界面)
     * @param func
     *            0 显示界面 1 新增界面 2 修改界面
     * @date: 2017-9-15
     */
    void initLayoutByFunc(int func) {
        // 每次切换时都重置选取的按钮
        resetPoint();
        switch (func) {
            case 0:
                etSearch.setVisibility(View.VISIBLE);
                rlPoint.setVisibility(View.GONE);
                llEquipInfo.setVisibility(View.GONE);
                llDisplay.setVisibility(View.GONE);
                llEdit.setVisibility(View.GONE);
                tvPost.setVisibility(View.GONE);

                tvDelEquip.setVisibility(View.VISIBLE);
                tvEditEquip.setText("编辑");
                tvSearch.setVisibility(View.VISIBLE);
                tvBuilding.setText(planBean.getBuilding());
                tvLayout.setText(planBean.getFloor_no());
                tvDelEquip.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // 删除该设备
                        deleteEquip();
                    }
                });

                tvEditEquip.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // 编辑设备
                        EquipDetailActivity.this.func = 2;
                        initLayoutByFunc(EquipDetailActivity.this.func);
                    }
                });

                ivPic.setClick_status(2);
                setChooseEquipViewStatus();
                if (planEquip != null) {
                    llEquipInfo.setVisibility(View.VISIBLE);
                    llDisplay.setVisibility(View.VISIBLE);
                }
                break;
            case 1:
                etSearch.setVisibility(View.GONE);
                rlPoint.setVisibility(View.VISIBLE);
                llEquipInfo.setVisibility(View.VISIBLE);
                llEdit.setVisibility(View.VISIBLE);
                llDisplay.setVisibility(View.GONE);
                tvPost.setText("新增");
                tvPost.setVisibility(View.VISIBLE);
                tvDelEquip.setVisibility(View.GONE);
                tvSearch.setVisibility(View.GONE);
                tvEditEquip.setVisibility(View.GONE);

                // 设置大楼名称、楼层
                tvBuilding.setText(planBean.getBuilding());
                tvLayout.setText(planBean.getFloor_no());

                resetEditData();
                // 获取设备类型
                getEquipType();
                // 获取系统类型
                getEquipSystemData();
                // 初始化控件
                setAddEquipViewStatus();
                break;
            case 2:
                etSearch.setVisibility(View.GONE);
                rlPoint.setVisibility(View.VISIBLE);
                llEquipInfo.setVisibility(View.VISIBLE);
                llEdit.setVisibility(View.VISIBLE);
                llDisplay.setVisibility(View.GONE);
                tvPost.setText("保存");
                tvPost.setVisibility(View.VISIBLE);
                tvDelEquip.setVisibility(View.GONE);
                tvSearch.setVisibility(View.GONE);
                tvEditEquip.setText("取消");
                setEditData();

                tvEditEquip.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // 取消编辑

                        // 判断是否有修改过点位的位置，有就还原图片的位置
                        if (tempUpdatePlanEquip != null) {
                            for (int i = 0; i < planBean.getEquipList().size(); i++) {
                                PlanEquip temp = planBean.getEquipList().get(i);
                                if (temp.getId()
                                        .equals(tempUpdatePlanEquip.getId())) {
                                    // 取消编辑时还原数据
                                    temp.setX_axis(tempUpdatePlanEquip.getX_axis());
                                    temp.setY_axis(tempUpdatePlanEquip.getY_axis());
                                    temp.setEquiptype(tempUpdatePlanEquip
                                            .getEquiptype());
                                    temp.setIcon_name(tempUpdatePlanEquip
                                            .getIcon_name());
                                    temp.setIcon(tempUpdatePlanEquip.getIcon());
                                }
                            }
                            tempUpdatePlanEquip = null;
                            ivPic.reset();
                            setChooseMaps(planBean.getEquipList(),
                                    planEquip.getId());
                        }

                        EquipDetailActivity.this.func = 0;
                        initLayoutByFunc(EquipDetailActivity.this.func);
                    }
                });

                // 获取设备类型
                getEquipType();
                // 获取系统类型
                getEquipSystemData();
                setUpdateEquipViewStatus();
                break;

            default:
                break;
        }
    }

    @OnClick({R.id.tvBack, R.id.tvSearch, R.id.tvPost, R.id.tvDelEquip,
            R.id.tvEditEquip, R.id.tvPoint, R.id.tvCancelPoint,
            R.id.ref0, R.id.ref1, R.id.ref2
    })
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.ref0:
//                ivPic.setDegree(0);
                ivPic.ref();
                break;
            case R.id.ref1:

                ivPic.setDegree(100);
                break;
            case R.id.ref2:

                ivPic.setDegree(255);
                break;
        }

        switch (func) {
            case 0:
                // 显示的情况
                switch (view.getId()) {
                    case R.id.tvBack:
                        finish();
                        break;
                    case R.id.tvSearch:
                        // 新增设备
                        Intent intent = new Intent(this, EquipDetailActivity.class);
                        intent.putExtra("func", 1);
                        intent.putExtra("floorId", floorId);
                        startActivityForResult(intent, ADD);
                        break;
                    default:
                        break;
                }
                break;
            case 1:
                // 新增的时候
                switch (view.getId()) {
                    case R.id.tvBack:
                    case R.id.tvSearch:
                        finish();
                        break;
                    case R.id.tvPost:
                        // 执行新增操作
                        if (CheckUtil.checkView(etGroup, state_system, state_equip, etEquipNo,
                                etEquipName, etEquipPosition, tvEditX, tvEditX,
                                state_equip_type_add, etEquipPrice)) {
                            addEquip();
                        }
                        break;
                    case R.id.tvPoint:
                        view.setVisibility(View.GONE);
                        llCanclePoint.setVisibility(View.VISIBLE);
                        ivPic.setClick_status(1);
                        break;
                    case R.id.tvCancelPoint:
                        llCanclePoint.setVisibility(View.GONE);
                        tvPoint.setVisibility(View.VISIBLE);
                        ivPic.setClick_status(0);
                        break;
                    default:
                        break;
                }
                break;
            case 2:
                // 编辑的时候
                switch (view.getId()) {
                    case R.id.tvBack:
                        finish();
                        break;
                    case R.id.tvPost:
                        // 执行保存操作
                        if (CheckUtil.checkView(state_system, state_equip, etEquipNo,
                                etEquipName, etEquipPosition, tvEditX, tvEditX,
                                state_equip_type_add, etEquipPrice)) {
                            updateEquip();
                        }
                        break;
                    case R.id.tvPoint:
                        view.setVisibility(View.GONE);
                        llCanclePoint.setVisibility(View.VISIBLE);
                        ivPic.setClick_status(3);
                        break;
                    case R.id.tvCancelPoint:
                        llCanclePoint.setVisibility(View.GONE);
                        tvPoint.setVisibility(View.VISIBLE);
                        ivPic.setClick_status(0);
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;
        }
    }

    Context getContext() {
        return this;
    }

    ;

    /**
     * @author: HYM
     * @Title: setEditData
     * @Description: TODO(设置编辑数据) void
     * @date: 2017-9-13
     */
    void setEditData() {
        etGroup.setText(tvGroup.getText().toString());
        etEquipNo.setText(tvEquipNo.getText().toString());
        etEquipName.setText(tvEquipName.getText().toString());
        etEquipPosition.setText(tvEquipPosition.getText().toString());
        tvEquipBrandEdit.setText(tvEquipBrand.getText().toString());
        tvEquipModelEdit.setText(tvEquipModel.getText().toString());
        etEquipPrice.setText(tvEquipPrice.getText().toString());
        tvEditX.setText(tvX.getText().toString());
        tvEditY.setText(tvY.getText().toString());
    }

    /**
     * @author: HYM
     * @Title: setDisplayData
     * @Description: TODO(把修改后的数据保存到查看的控件里面) void
     * @date: 2017-9-15
     */
    void setDisplayData() {
        tvGroup.setText(etGroup.getText().toString());
        tvSystem.setText(new Gson().fromJson(
                state_system.getSelectedItem().toString(),
                EquipSystemBean.class).getInspection_name());
        tvSystem.setTag(new Gson().fromJson(
                state_system.getSelectedItem().toString(),
                EquipSystemBean.class).getInspection_id());
        tvEquip.setText(new Gson().fromJson(
                state_equip.getSelectedItem().toString(),
                SystemEquipItemBean.class).getEquipment_name());
        tvEquip.setTag(new Gson().fromJson(
                state_equip.getSelectedItem().toString(),
                SystemEquipItemBean.class).getEquipment_id());
        tvEquipNo.setText(etEquipNo.getText().toString());
        tvEquipName.setText(etEquipName.getText().toString());
        tvEquipPosition.setText(etEquipPosition.getText().toString());
        tvEquipBrand.setText(tvEquipBrandEdit.getText().toString());
        tvEquipModel.setText(tvEquipModelEdit.getText().toString());
        tvEquipPrice.setText(etEquipPrice.getText().toString());
        tvX.setText(tvEditX.getText().toString());
        tvY.setText(tvEditY.getText().toString());
    }

    /**
     * @author: HYM
     * @Title: resetEditData
     * @Description: TODO(重置编辑数据) void
     * @date: 2017-9-13
     */
    void resetEditData() {
        etEquipNo.setText("");
        etEquipName.setText("");
        etEquipPosition.setText("");
        // etEquipType.setText("");
        etEquipPrice.setText("");
    }

    /**
     * @author: HYM
     * @Title: setMaps
     * @Description: TODO(对平面图设置图标，不区分是否选中的状态)
     * @param goodsMaps
     *            void
     * @date: 2017-9-15
     */
    void setMaps(final List<PlanEquip> goodsMaps) {

        System.out.println("size:" + goodsMaps.size());

        final ArrayList<PositionPoint> positionPoints = new ArrayList<PositionPoint>();
        for (int i = 0; i < goodsMaps.size(); i++) {
            final int temp = i;
            final PositionPoint point = new PositionPoint();
            point.setPointX(goodsMaps.get(temp).getX_axis());
            point.setPointY(goodsMaps.get(temp).getY_axis());
            point.setId(goodsMaps.get(temp).getId());
            point.setIcon(goodsMaps.get(temp).getIcon());
            point.setAlarm_status(goodsMaps.get(temp).getAlarm_status());
//            if (i == 0 || i == 1 || i == goodsMaps.size() - 2 || i == goodsMaps.size() - 3) {
//                point.setWx(true);
//                point.setAlpha(30);
//            } else {
//                point.setAlpha(255);
//            }



            if (goodsMaps.get(temp).getAlarm_status()==0) {
                point.setAlpha(255);
            } else {
                point.setWx(true);
                point.setAlpha(30);
            }

            point.setColor(R.color.half_half_half_transparency);
            point.setSelect(false);
            positionPoints.add(point);

//            Glide.with(this.getContext()).load(goodsMaps.get(i).getIcon())
//                    .asBitmap().into(new SimpleTarget<Bitmap>() {
//                @Override
//                public void onResourceReady(Bitmap bitmap,
//                                            GlideAnimation glideAnimation) {
////                        ivPic.loadWaterList(bitmap, goodsMaps.get(temp)
////                                .getX_axis(), goodsMaps.get(temp)
////                                .getY_axis(), false);
//                }
//            }); // 方法中设置asBitmap可以设置回调类型
        }
        ivPic.setPositionPoints(positionPoints,false);
    }

    /**
     * @author: HYM
     * @Title: setChooseMaps
     * @Description: TODO(对平面图设置图标，对选中的设置高亮)
     * @param mGoodsMaps
     * @param currentEquipId
     *            void
     * @date: 2017-9-15
     */
    void setChooseMaps(final List<PlanEquip> mGoodsMaps, String currentEquipId) {
        PlanEquip currentCopy = null;
        final ArrayList<PositionPoint> positionPoints = new ArrayList<PositionPoint>();

        for (int i = 0; i < mGoodsMaps.size(); i++) {

            final PlanEquip om = mGoodsMaps.get(i);

            final int temp = i;
            final PositionPoint point = new PositionPoint();
            point.setPointX(mGoodsMaps.get(temp).getX_axis());
            point.setPointY(mGoodsMaps.get(temp).getY_axis());
            point.setId(mGoodsMaps.get(temp).getId());
            point.setIcon(mGoodsMaps.get(temp).getIcon());
            point.setAlarm_status(mGoodsMaps.get(temp).getAlarm_status());
            if (mGoodsMaps.get(temp).getAlarm_status()==0) {
                point.setAlpha(255);
            } else {
                point.setWx(true);
                point.setAlpha(30);
            }
            // 判断是否是当前的点
            if (om.getId().equals(currentEquipId)) {
                currentCopy = mGoodsMaps.get(i);
                point.setSelect(true);
                point.setWx(false);
                point.setColor(Color.RED);
                point.setAlpha(255);
                continue;
            } else {
                point.setSelect(false);
                point.setColor(R.color.half_half_half_transparency);
//
                Glide.with(this.getContext()).load(om.getIcon()).asBitmap()
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(Bitmap bitmap,
                                                        GlideAnimation glideAnimation) {

//                                if (!point.isWx()) {
//                                    ivPic.loadWaterList(bitmap, om.getX_axis(),
//                                            om.getY_axis(), false);
//                                }

                                int alpha;
                                if (point.isWx()) {
                                    alpha = 30;
                                } else {
                                    alpha = 255;
                                }

                                int color=R.color.half_half_half_transparency;

                                ivPic.loadWaterList(bitmap, om.getX_axis(),
                                        om.getY_axis(), false,color,alpha);

                            }
                        }); // 方法中设置asBitmap可以设置回调类型
            }
            positionPoints.add(point);
        }

        final PlanEquip current = currentCopy;

        // 最后加载当前选中的
        Glide.with(this.getContext()).load(current.getIcon()).asBitmap()
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap bitmap,
                                                GlideAnimation glideAnimation) {
                        ivPic.loadWaterList(bitmap, current.getX_axis(),
                                current.getY_axis(), true, Color.RED,255);

                        ivPic.setPositionPoints(positionPoints,true);
                    }
                });
//        ivPic.setPositionPoints(positionPoints,false);
    }

    /**
     * @author: HYM
     * @Title: addEquip
     * @Description: TODO(往服务器数据库新增设备) void
     * @date: 2017-9-15
     */
    void addEquip() {
        final String systemType = new Gson().fromJson(
                state_system.getSelectedItem().toString(),
                EquipSystemBean.class).getInspection_id();
        final String systemEquipItemType = new Gson().fromJson(
                state_equip.getSelectedItem().toString(),
                SystemEquipItemBean.class).getEquipment_id();


        final String equipType = new Gson().fromJson(
                state_equip_type_add.getSelectedItem().toString(),
                EquipType.class).getId();
        final String icon = new Gson().fromJson(
                state_equip_type_add.getSelectedItem().toString(),
                EquipType.class).getIcon_url();
        final String icon_name = new Gson().fromJson(
                state_equip_type_add.getSelectedItem().toString(),
                EquipType.class).getIcon_name();

        Map<String, String> params = new HashMap<String, String>();
        params.put(AppConfig.ACCESS_TOKEN,
                UserInfoUtil.getCurrentUserAccessToken());
        params.put("company_id", planBean.getCompany_id());
        params.put("build_id", planBean.getBuild_id());
        params.put("floor_id", floorId);
        params.put("goodsProduct", tvEquipBrand.getText().toString());
        params.put("goodsType", tvEquipModel.getText().toString());
        params.put("equipType", equipType);
        params.put("bh_code", etEquipNo.getText().toString());
        params.put("goodsName", etEquipName.getText().toString());
        params.put("goodsPrice", etEquipPrice.getText().toString());
        params.put("place", etEquipPosition.getText().toString());
        params.put("x_axis", tvEditX.getText().toString());
        params.put("y_axis", tvEditY.getText().toString());
        params.put("inspection_id", systemType);
        params.put("equipment_id", systemEquipItemType);
        params.put("groupno", etGroup.getText().toString());

        dialog = showDialog(dialog);
        VolleyRequest.StringRequestPost(this,
                WebApi.MAINTENANCE_OWNER_EQUIP_ADD, "", params,
                new StringInterface(this) {

                    @Override
                    public void stringSuccess(String result) {
                        try {
                            JSONObject obj = new JSONObject(result);
                            String status = obj.getString("status");
                            String info = obj.getString("info");
                            if (status.equals("200")) {
                                ToastUtil.getInstances().showMsg(info);
                                // 通知之前的平面图详情刷新设备点
                                Intent intent = new Intent();
                                PlanEquip data = new PlanEquip();
                                data.setBh_code(etEquipNo.getText().toString());
                                // 需要由后台返回插入的ID
                                String equipment_id = obj.getString("data");
                                data.setId(equipment_id);
                                data.setEquiptype(equipType);
                                data.setGoodsname(etEquipName.getText()
                                        .toString());
                                data.setIcon(icon);
                                data.setIcon_name(icon_name);
                                data.setX_axis(Float.parseFloat(tvEditX
                                        .getText().toString()));
                                data.setY_axis(Float.parseFloat(tvEditY
                                        .getText().toString()));
                                data.setGroupno(etGroup.getText().toString());
                                data.setInspection_id(systemType);
                                data.setEquipment_id(systemEquipItemType);
                                intent.putExtra("newEquip",
                                        new Gson().toJson(data));
                                setResult(RESULT_OK, intent);
                                finish();
                            } else if (status.equals("404")
                                    && info.contains("尚未登录")) {
                                ToastUtil.getInstances().showMsg(
                                        "尚未登录或该用户在其他地方登录，请重新登录！");
                                // 清除环信登录
                                UserHelp.getInstance().clearUser(
                                        AppBase.Context, true);
                                return;
                            } else {
                                ToastUtil.getInstances().showMsg(info);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        } finally {
                            hideDialog(dialog);
                        }

                    }

                    @Override
                    public void stringError(VolleyError error) {
                        hideDialog(dialog);
                        ToastUtil.getInstances().showMsg(
                                R.string.err_http_response);
                    }
                });
    }

    /**
     * @author: HYM
     * @Title: updateEquip
     * @Description: TODO(往服务器数据库修改设备点信息) void
     * @date: 2017-9-15
     */
    void updateEquip() {
        Map<String, String> params = new HashMap<String, String>();
        params.put(AppConfig.ACCESS_TOKEN,
                UserInfoUtil.getCurrentUserAccessToken());
        params.put("id", planEquip.getId());
        params.put("bh_code", etEquipNo.getText().toString());
        params.put("goodsName", etEquipName.getText().toString());
        params.put("place", etEquipPosition.getText().toString());
        params.put("x_axis", tvEditX.getText().toString());
        params.put("y_axis", tvEditY.getText().toString());
        params.put("goodsProduct", tvEquipBrandEdit.getText().toString());
        params.put("goodsType", tvEquipModelEdit.getText().toString());
        params.put(
                "equipType",
                new Gson().fromJson(
                        state_equip_type_add.getSelectedItem().toString(),
                        EquipType.class).getId());
        params.put("goodsPrice", etEquipPrice.getText().toString());
        params.put(
                "inspection_id",
                new Gson().fromJson(state_system.getSelectedItem().toString(),
                        EquipSystemBean.class).getInspection_id());
        params.put(
                "equipment_id",
                new Gson().fromJson(state_equip.getSelectedItem().toString(),
                        SystemEquipItemBean.class).getEquipment_id());
        params.put("groupno", etGroup.getText().toString());

        dialog = showDialog(dialog);
        VolleyRequest.StringRequestPost(this,
                WebApi.MAINTENANCE_OWNER_EQUIP_UPDATE, "", params,
                new StringInterface(this) {

                    @Override
                    public void stringSuccess(String result) {
                        try {
                            JSONObject obj = new JSONObject(result);
                            String status = obj.getString("status");
                            String info = obj.getString("info");
                            if (status.equals("200")) {
                                ToastUtil.getInstances().showMsg(info);
                                // 重置临时的设备点对象
                                tempUpdatePlanEquip = null;
                                func = 0;
                                initLayoutByFunc(func);
                                setDisplayData();
                            } else if (status.equals("404")
                                    && info.contains("尚未登录")) {
                                ToastUtil.getInstances().showMsg(
                                        "尚未登录或该用户在其他地方登录，请重新登录！");
                                // 清除环信登录
                                UserHelp.getInstance().clearUser(
                                        AppBase.Context, true);
                                return;
                            } else {
                                ToastUtil.getInstances().showMsg(info);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        } finally {
                            hideDialog(dialog);
                        }

                    }

                    @Override
                    public void stringError(VolleyError error) {
                        hideDialog(dialog);
                        ToastUtil.getInstances().showMsg(
                                R.string.err_http_response);
                    }
                });
    }

    /**
     * @author: HYM
     * @Title: deleteEquip
     * @Description: TODO(往服务器数据库删除设备点) void
     * @date: 2017-9-15
     */
    void deleteEquip() {
        if (planEquip == null) {
            return;
        }
        Map<String, String> params = new HashMap<String, String>();
        params.put(AppConfig.ACCESS_TOKEN,
                UserInfoUtil.getCurrentUserAccessToken());
        params.put("equipment_id", planEquip.getId());

        dialog = showDialog(dialog);
        VolleyRequest.StringRequestPost(this,
                WebApi.MAINTENANCE_OWNER_EQUIP_DELETE, "", params,
                new StringInterface(this) {

                    @Override
                    public void stringSuccess(String result) {
                        try {
                            JSONObject obj = new JSONObject(result);
                            String status = obj.getString("status");
                            String info = obj.getString("info");
                            if (status.equals("200")) {
                                ToastUtil.getInstances().showMsg(info);
                                // 从点位列表中删除掉这个
                                for (int i = 0; i < planBean.getEquipList()
                                        .size(); i++) {
                                    PlanEquip temp = planBean.getEquipList()
                                            .get(i);
                                    if (temp.getId().equals(planEquip.getId())) {
                                        planBean.getEquipList().remove(i);
                                        planEquip = null;
                                        break;
                                    }
                                }
                                // 刷新平面图的设备点
                                refreshPic();

                                func = 0;
                                initLayoutByFunc(func);

                            } else if (status.equals("404")
                                    && info.contains("尚未登录")) {
                                ToastUtil.getInstances().showMsg(
                                        "尚未登录或该用户在其他地方登录，请重新登录！");
                                // 清除环信登录
                                UserHelp.getInstance().clearUser(
                                        AppBase.Context, true);
                                return;
                            } else {
                                ToastUtil.getInstances().showMsg(info);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        } finally {
                            hideDialog(dialog);
                        }

                    }

                    @Override
                    public void stringError(VolleyError error) {
                        hideDialog(dialog);
                        ToastUtil.getInstances().showMsg(
                                R.string.err_http_response);
                    }
                });
    }

    /**
     * @author: HYM
     * @Title: getEquipSystemData
     * @Description: TODO(获取设备系统) void
     * @date: 2017-10-24
     */
    private void getEquipSystemData() {
        Map<String, String> params = new HashMap<String, String>();
        params.put(AppConfig.ACCESS_TOKEN,
                UserInfoUtil.getCurrentUserAccessToken());
        params.put("company_id", planBean.getCompany_id());

        VolleyRequest.StringRequestPost(this,
                WebApi.MAINTENANCE_OWNER_EQUIP_SYSTEM_LIST, "", params,
                new StringInterface(this) {

                    @Override
                    public void stringSuccess(String result) {
                        try {
                            JSONObject obj = new JSONObject(result);
                            String status = obj.getString("status");
                            String info = obj.getString("info");
                            if (status.equals("200")) {
                                String data = obj.getString("data");
                                List<EquipSystemBean> temp = new Gson()
                                        .fromJson(
                                                data,
                                                new TypeToken<List<EquipSystemBean>>() {
                                                }.getType());
                                if (temp == null || temp.size() <= 0) {
                                    ToastUtil.getInstances()
                                            .showMsg("请先新增系统类型");
                                } else {
                                    resetSystem();
                                    systemDataList.addAll(temp);
                                    stateSystemAdapter.notifyDataSetChanged();

                                    // 如果是编辑状态，则根据系统的默认类型来默认选中系统类型下拉的值
                                    if (func == 2) {
                                        state_system
                                                .setSelection(getPositionBySystem(
                                                        tvSystem.getTag()
                                                                .toString(),
                                                        systemDataList));
                                    }
                                }
                            } else {
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        } finally {
                        }

                    }

                    @Override
                    public void stringError(VolleyError error) {
                    }
                });
    }

    private void getSystemEquipItemData(String id) {
        Map<String, String> params = new HashMap<String, String>();
        params.put(AppConfig.ACCESS_TOKEN,
                UserInfoUtil.getCurrentUserAccessToken());
        params.put("inspection_id", id);

        VolleyRequest.StringRequestPost(this,
                WebApi.MAINTENANCE_OWNER_SYSTEM_EQUIP_ITEM_LIST, "", params,
                new StringInterface(this) {

                    @Override
                    public void stringSuccess(String result) {
                        try {
                            JSONObject obj = new JSONObject(result);
                            String status = obj.getString("status");
                            String info = obj.getString("info");
                            if (status.equals("200")) {
                                String data = obj.getString("data");
                                List<SystemEquipItemBean> temp = new Gson()
                                        .fromJson(
                                                data,
                                                new TypeToken<List<SystemEquipItemBean>>() {
                                                }.getType());
                                if (temp == null || temp.size() <= 0) {
                                    ToastUtil.getInstances().showMsg(
                                            "请先新增系统设备类型");
                                } else {
                                    resetSystemEquipItem();
                                    systemEquipItemDataList.addAll(temp);
                                    stateEquipItemAdapter
                                            .notifyDataSetChanged();
                                    // 如果是编辑状态，则根据设备的默认类型来默认选中设备类型下拉的值
                                    if (func == 2) {
                                        state_equip
                                                .setSelection(getPositionByEquip(
                                                        tvEquip.getTag()
                                                                .toString(),
                                                        systemEquipItemDataList));
                                    }
                                }
                            } else {
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        } finally {
                        }

                    }

                    @Override
                    public void stringError(VolleyError error) {
                    }
                });
    }

    /**
     * @author: HYM
     * @Title: getEquipType
     * @Description: TODO(获取设备类型，用于下拉选择项) void
     * @date: 2017-9-15
     */
    void getEquipType() {
        Map<String, String> params = new HashMap<String, String>();
        params.put(AppConfig.ACCESS_TOKEN,
                UserInfoUtil.getCurrentUserAccessToken());

        VolleyRequest.StringRequestPost(getContext(),
                WebApi.MAINTENANCE_OWNER_EQUIP_TYPE, "", params,
                new StringInterface(getContext()) {

                    @Override
                    public void stringSuccess(String result) {
                        try {
                            JSONObject obj = new JSONObject(result);
                            String status = obj.getString("status");
                            String info = obj.getString("info");
                            if (status.equals("200")) {
                                String data = obj.getString("data");
                                List<EquipType> temp = new Gson().fromJson(
                                        data, new TypeToken<List<EquipType>>() {
                                        }.getType());
                                setEquipType(temp);
                            } else if (status.equals("404")
                                    && info.contains("尚未登录")) {
                                ToastUtil.getInstances().showMsg(
                                        "尚未登录或该用户在其他地方登录，请重新登录！");
                                // 清除环信登录
                                UserHelp.getInstance().clearUser(
                                        AppBase.Context, true);
                                return;
                            } else {
                                ToastUtil.getInstances().showMsg(info);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        } finally {
                        }
                    }

                    @Override
                    public void stringError(VolleyError error) {
                        ToastUtil.getInstances().showMsg(
                                R.string.err_http_response);
                    }
                });
    }

    /**
     * @author: HYM
     * @Title: resetSystem
     * @Description: TODO(重置设备系统列表的数据) void
     * @date: 2017-10-24
     */
    private void resetSystem() {
        systemDataList.clear();
        EquipSystemBean equipSystemBean = new EquipSystemBean();
        equipSystemBean.setInspection_id("");
        equipSystemBean.setInspection_name("请选择");
        systemDataList.add(equipSystemBean);
        if (stateSystemAdapter != null) {
            stateSystemAdapter.notifyDataSetChanged();
        }
    }

    /**
     * @author: HYM
     * @Title: resetSystem
     * @Description: TODO(重置系统-设备列表的数据) void
     * @date: 2017-10-24
     */
    private void resetSystemEquipItem() {
        systemEquipItemDataList.clear();
        SystemEquipItemBean systemEquipItemBean = new SystemEquipItemBean();
        systemEquipItemBean.setEquipment_id("");
        systemEquipItemBean.setEquipment_name("请选择");
        systemEquipItemDataList.add(systemEquipItemBean);
        if (stateEquipItemAdapter != null) {
            stateEquipItemAdapter.notifyDataSetChanged();
        }
    }

    /**
     * @author: HYM
     * @Title: resetEquipType
     * @Description: TODO(重置设备列表下拉选项) void
     * @date: 2017-9-15
     */
    private void resetEquipType() {
        equipTypeDataList.clear();
        EquipType et = new EquipType();
        et.setId("");
        et.setIcon_name("请选择");
        et.setIcon_url("");
        equipTypeDataList.add(et);
        if (stateEquipTypeAdapter != null) {
            stateEquipTypeAdapter.notifyDataSetChanged();
        }
    }

    /**
     * @author: HYM
     * @Title: setEquipType
     * @Description: TODO(填充设备点位到下拉列表)
     * @param list
     *            void
     * @date: 2017-9-15
     */
    void setEquipType(List<EquipType> list) {
        resetEquipType();
        if (list != null && list.size() > 0) {
            equipTypeDataList.addAll(list);
            stateEquipTypeAdapter.notifyDataSetChanged();
        }

        // 如果是编辑状态，则根据设备点位的设备类型来默认选中设备类型下拉的值
        if (func == 2) {
            state_equip_type_add.setSelection(getPositionByEquipType(
                    planEquip.getEquiptype(), equipTypeDataList));
        }
    }

    /**
     * @author: HYM
     * @Title: getEquipInfo
     * @Description: TODO(获取设备点的具体信息，默认状态下点平面图的点位时调用)
     * @param equipment_id
     *            void
     * @date: 2017-9-15
     */
    void getEquipInfo(String equipment_id) {
        Map<String, String> params = new HashMap<String, String>();
        params.put(AppConfig.ACCESS_TOKEN,
                UserInfoUtil.getCurrentUserAccessToken());
        params.put("id", equipment_id);

        dialog = showDialog(dialog);

        VolleyRequest.StringRequestPost(getContext(),
                WebApi.MAINTENANCE_OWNER_EQUIP_INFO, "", params,
                new StringInterface(getContext()) {

                    @Override
                    public void stringSuccess(String result) {
                        try {
                            JSONObject obj = new JSONObject(result);
                            String status = obj.getString("status");
                            String info = obj.getString("info");
                            if (status.equals("200")) {
                                String data = obj.getString("data");
                                planEquip = new Gson().fromJson(data,
                                        PlanEquip.class);



                                for (int i = 0; i < planBean.getEquipList().size(); i++) {


                                    if (planEquip.getX_axis() == planBean.getEquipList().get(i).getX_axis()) {
                                        planEquip.setAlarm_status(planBean.getEquipList().get(i).getAlarm_status());
//                                        System.out.println("id:" + planBean.getEquipList().get(i).getStatus());

                                    }

                                }


                                showEquipInfo(planEquip);
                            } else if (status.equals("404")
                                    && info.contains("尚未登录")) {
                                ToastUtil.getInstances().showMsg(
                                        "尚未登录或该用户在其他地方登录，请重新登录！");
                                // 清除环信登录
                                UserHelp.getInstance().clearUser(
                                        AppBase.Context, true);
                                return;
                            } else {
                                ToastUtil.getInstances().showMsg(info);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        } finally {
                            hideDialog(dialog);
                        }
                    }

                    @Override
                    public void stringError(VolleyError error) {
                        hideDialog(dialog);
                        ToastUtil.getInstances().showMsg(
                                R.string.err_http_response);
                    }
                });
    }

    /**
     * @author: HYM
     * @Title: showEquipInfo
     * @Description: TODO(显示设备点位的信息)
     * @param planEquip
     *            void
     * @date: 2017-9-15
     */
    void showEquipInfo(PlanEquip planEquip) {
        llEquipInfo.setVisibility(View.VISIBLE);
        llDisplay.setVisibility(View.VISIBLE);
        llEdit.setVisibility(View.GONE);
        tvGroup.setText(planEquip.getGroupno());
        tvSystem.setText(planEquip.getInspection_name());
        tvSystem.setTag(planEquip.getInspection_id());
        tvEquip.setText(planEquip.getEquipment_name());
        tvEquip.setTag(planEquip.getEquipment_id());
        tvEquipNo.setText(planEquip.getBh_code());
        tvEquipName.setText(planEquip.getGoodsname());
        tvX.setText(planEquip.getX_axis() + "");
        tvY.setText(planEquip.getY_axis() + "");
        tvEquipType.setText(planEquip.getIcon_name());
        tvEquipPrice.setText(planEquip.getGoodsprice());
        tvEquipPosition.setText(planEquip.getPlace());
        tvEquipBrand.setText(planEquip.getGoodsproduct());
        tvEquipModel.setText(planEquip.getGoodstype());
        tvStatus.setVisibility(View.VISIBLE);


        //异常状态
        String status="";
        if (planEquip.getAlarm_status() == 0) {
            status = "正常";
        } else if (planEquip.getAlarm_status() == 1) {
            status = "异常";
        } else if (planEquip.getAlarm_status() == 2) {
            status = "火警";
        }

        tvStatus.setText(status+"");
    }

    /**
     * @author: HYM
     * @Title: getPositionByEquipType
     * @Description: TODO(返回某个设备类型在设备类型列表的值，用于设置默认的下拉项)
     * @param typeId
     * @param list
     * @return int
     * @date: 2017-9-15
     */
    int getPositionByEquipType(String typeId, List<EquipType> list) {
        for (int i = 0; i < list.size(); i++) {
            EquipType tl = list.get(i);
            if (tl.getId().equals(typeId)) {
                return i;
            }
        }
        // 默认时
        return 0;
    }

    /**
     * @author: HYM
     * @Title: getPositionBySystem
     * @Description: TODO(获取查看的默认选中值，被选中)
     * @param list
     * @return int
     * @date: 2017-10-24
     */
    int getPositionBySystem(String systemId, List<EquipSystemBean> list) {
        for (int i = 0; i < list.size(); i++) {
            EquipSystemBean tl = list.get(i);
            if (tl.getInspection_id().equals(systemId)) {
                return i;
            }
        }
        // 默认时
        return 0;
    }

    int getPositionByEquip(String equipId, List<SystemEquipItemBean> list) {
        for (int i = 0; i < list.size(); i++) {
            SystemEquipItemBean tl = list.get(i);
            if (tl.getEquipment_id().equals(equipId)) {
                return i;
            }
        }
        // 默认时
        return 0;
    }

    /**
     * @author: HYM
     * @Title: resetPoint
     * @Description: TODO(重置点位选取的按钮为可以选择状态) void
     * @date: 2017-9-15
     */
    void resetPoint() {
        llCanclePoint.setVisibility(View.GONE);
        rlPoint.setVisibility(View.VISIBLE);
        tvPoint.setVisibility(View.VISIBLE);
        ivPic.setClick_status(0);
    }

    /**
     * @author: HYM
     * @Title: refreshPic
     * @Description: TODO(刷新删除的平面图) void
     * @date: 2017-9-15
     */
    void refreshPic() {
        ivPic.reset();
        setMaps(planBean.getEquipList());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ADD) {
            if (resultCode == RESULT_OK) {
                // 如果是新增设备完成，刷新设备点位列表的数据，刷新平面图里面的点位
                String dataGson = data.getStringExtra("newEquip");
                PlanEquip planEquip = new Gson().fromJson(dataGson,
                        PlanEquip.class);
                planBean.getEquipList().add(planEquip);
                refreshPic();
            }
        }
    }

    void searchCode() {
        Map<String, String> params = new HashMap<String, String>();
        params.put(AppConfig.ACCESS_TOKEN,
                UserInfoUtil.getCurrentUserAccessToken());
        params.put("bh_code", etSearch.getText().toString().trim());
        params.put("floor_id", floorId);
        dialog = showDialog(dialog);
        VolleyRequest.StringRequestPost(getContext(),
                WebApi.MAINTENANCE_OWNER_EQUIP_INFO, "", params,
                new StringInterface(getContext()) {

                    @Override
                    public void stringSuccess(String result) {
                        try {
                            JSONObject obj = new JSONObject(result);
                            String status = obj.getString("status");
                            String info = obj.getString("info");
                            if (status.equals("200")) {
                                String data = obj.getString("data");
                                planEquip = new Gson().fromJson(data,
                                        PlanEquip.class);
                                if (planEquip == null) {
                                    ToastUtil.getInstances().showMsg("没有查到该编号");
                                } else {
                                    ivPic.reset();
                                    setChooseMaps(planBean.getEquipList(),
                                            planEquip.getId());
                                    showEquipInfo(planEquip);
                                }

                            } else if (status.equals("404")
                                    && info.contains("尚未登录")) {
                                ToastUtil.getInstances().showMsg(
                                        "尚未登录或该用户在其他地方登录，请重新登录！");
                                // 清除环信登录
                                UserHelp.getInstance().clearUser(
                                        AppBase.Context, true);
                                return;
                            } else {
                                ToastUtil.getInstances().showMsg(info);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        } finally {
                            hideDialog(dialog);
                        }
                    }

                    @Override
                    public void stringError(VolleyError error) {
                        hideDialog(dialog);
                        ToastUtil.getInstances().showMsg(
                                R.string.err_http_response);
                    }
                });
    }
}
